﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Configuration;

using MyLibrary.Tools;
using MyLibrary.TaskPlan.Service;

namespace TaskPlanMonitor
{
    /// <summary>
    /// 计划任务运行监视程序
    /// </summary>
    partial class TaskPlanMonitorService : ServiceBase
    {
        private TaskScheduleMonitor monitor = null;

        public TaskPlanMonitorService()
        {
            InitializeComponent();
            try
            {
                monitor = new TaskScheduleMonitor();
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("TaskPlanMonitor", ex.Message);
            }
        }

        protected override void OnStart(string[] args)
        {
            string assemblyPath = AppDomain.CurrentDomain.BaseDirectory + @"\ServiceConfig.xml";
            if (!System.IO.File.Exists(assemblyPath))
            {
                System.Diagnostics.EventLog.WriteEntry("TaskPlanMonitor", "配置文件不存在");
                return;
            }
            string connstring = CommonTools.GetConfigSectionNoEncrypt("DatabaseConnstring", assemblyPath);
            if (String.IsNullOrEmpty(connstring))
            {
                System.Diagnostics.EventLog.WriteEntry("TaskPlanMonitor", "未配置连接字符串");
                return;
            }
            //读取连接字符串
            monitor.DBConnstring = connstring;
            monitor.Start();
        }

        protected override void OnStop()
        {
            monitor.Stop();
        }

        protected override void OnPause()
        {
            monitor.Pause();
        }

        protected override void OnContinue()
        {
            monitor.Start();
        }
    }
}
